﻿using System;
using System.Collections.Generic;

using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Windows.Forms;
using TCM.DbContextt;
using TCM.Models;

namespace TCM.Forms
{
    public partial class CasesList : Form
    {

        BaseContext db = new BaseContext();

        public CasesList()
        {
            InitializeComponent();
            LoadTable();
            db.Categories.Load();
            db.Cases.Load();
            
        }

        private void LoadTable()
        {
            dataGridView1.DataSource = db.Cases.Local.ToBindingList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CasesManager addCaseForm = new CasesManager();
            DialogResult result = addCaseForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
                return;

            Case cAse = new Case();
            cAse.CaseId = Guid.NewGuid();
            cAse.Name = addCaseForm.textBox1.Text;
            cAse.Content = addCaseForm.richTextBox1.Text;
            if (addCaseForm.comboBox1.SelectedValue != null)
            {
                cAse.CategoryId = (Guid) addCaseForm.comboBox1.SelectedValue;
            }

            db.Cases.Add(cAse);
            db.SaveChanges();

            MessageBox.Show("Новый объект добавлен");
        }

        // редактирование
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int index = dataGridView1.SelectedRows[0].Index;
                Guid id = Guid.Empty;
                bool converted = Guid.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                if (converted == false)
                    return;

                Case cAse = db.Cases.Find(id);

                CasesManager addCaseForm = new CasesManager();


                var Categories = db.Categories.ToList();
                addCaseForm.comboBox1.DataSource = Categories;
                addCaseForm.comboBox1.DisplayMember = "Name";

                addCaseForm.comboBox1.SelectedItem = cAse.Category;
                addCaseForm.textBox1.Text = cAse.Name;
                addCaseForm.richTextBox1.Text = cAse.Content;

                DialogResult result = addCaseForm.ShowDialog(this);

                if (result == DialogResult.Cancel)
                    return;

                cAse.Name = addCaseForm.textBox1.Text;
                cAse.Content = addCaseForm.richTextBox1.Text;
                if (addCaseForm.comboBox1.SelectedValue != null)
                {
                    cAse.Category = (Category)addCaseForm.comboBox1.SelectedItem;
                }

                db.Entry(cAse).State = EntityState.Modified;
                db.SaveChanges();

                MessageBox.Show("Объект обновлен");
                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int index = dataGridView1.SelectedRows[0].Index;
                Guid id = Guid.Empty;
                bool converted = Guid.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                if (converted == false)
                    return;

                Case cAse = db.Cases.Find(id);
                db.Cases.Remove(cAse);
                db.SaveChanges();

                MessageBox.Show("Объект удален");
            }
        }
    }
}
